package com.ggg.clothesshop.controller;

import com.ggg.clothesshop.model.User;
import com.ggg.clothesshop.service.UserService;
import com.ggg.clothesshop.utils.JWT;
import com.ggg.clothesshop.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    //由Id搜索用户
    @ResponseBody
    @RequestMapping("/selectById")
    public User selectById(int userId){

        return  userService.selectUserById(userId);
    }

    //由用户名密码搜索用户（登陆
    @ResponseBody
    @RequestMapping("/selectByPassword")
    public ResponseData selectByPassword(HttpServletResponse response, User user){

        user = userService.selectUserByPassword(user);
        if(user!=null){
            ResponseData responseData = ResponseData.ok();
            //将登陆信息生成JWT
            responseData.putDataValue("user", user);
            String token = JWT.sign(user, 7L * 24L * 3600L * 1000L);
            if (token != null) {
                responseData.putDataValue("token", token);
            }

            //防止登陆标记Token
            Cookie cookie = new Cookie("gggtoken", token);
            cookie.setPath("/");
            response.addCookie(cookie);

            return responseData;
        }
        ResponseData responseData = ResponseData.failLogin();
        return responseData;
    }

    //验证登录状态
    @ResponseBody
    @RequestMapping("/proveMe")
    public ResponseData proveMe(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for(int i=0; cookies!=null && i<cookies.length; i++){
            if("gggtoken".equals(cookies[i].getName())){
                String token = cookies[i].getValue();
                User user = JWT.unsign(token, User.class);
                //判断是否拿到过期的验证
                if(user != null) {
                    return ResponseData.ok().putDataValue("user", user);
                }

            }
        }
        return ResponseData.failLogin();
    }

    //注销
    @ResponseBody
    @RequestMapping("/removeMe")
    public Map removeMe(HttpServletResponse response){
        Map map = new HashMap();
        map.put("status",200);
        map.put("data","注销成功");
        Cookie killMyCookie = new Cookie("gggtoken", null);
        killMyCookie.setMaxAge(0);
        killMyCookie.setPath("/");
        response.addCookie(killMyCookie);
        return map;
    }

    //搜索一页用户信息
    @ResponseBody
    @RequestMapping("/selectByPage")
    public Object selectByPage(
            @RequestParam(name = "pageNum", required = false, defaultValue = "1")
                    int pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                    int pageSize){
        return userService.selectByPage(pageNum,pageSize);
    }

    //检测用户是否已经存在
    @ResponseBody
    @RequestMapping("/checkUser")
    public Map checkUser(User user){
        Map map = new HashMap();
        map.put("status",500);
        map.put("data","对不起,注册的账号已存在！");
        //检测是否已存在账号
        if(userService.selectByAccount(user.getAccount())==null) {
            map.put("data","账号可用");
            map.put("status",200);
        }
        return map;
    }

    //注册一个用户
    @ResponseBody
    @RequestMapping("/addUser")
    public Map addUser(User user){
        Map map = new HashMap();
        map.put("status",500);
        map.put("data","对不起,注册的账号已存在！");
        //检测是否已存在账号
        if(userService.selectByAccount(user.getAccount())==null) {
            //检测是否添加成功
            if(userService.addUser(user)==1) {
                map.put("data", "注册成功");
                map.put("status", 200);
            }
        }
        return map;
    }

}
